﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using PPI.Data;
using PPI.Entities;
using PPI.Entities.Interface;
using PPI.Mobile;
using VMind.Global;

namespace PPI.ServiceLogic
{
    public class LVendor_Project : IMobileTable
    {
        public List<int> Vendor_ProjectIdsToFilter = new List<int>();

        public static EVendor_Project SelectData(DataRow _DataRow)
        {
            EVendor_Project _Vendor_Project = new EVendor_Project();
            _Vendor_Project.ActualCost = ConvertHelper.Todouble(_DataRow["ActualCost"]);
            _Vendor_Project.ActualEndDate = ConvertHelper.ToDateTime(_DataRow["ActualEndDate"]);
            _Vendor_Project.ActualStartDate = ConvertHelper.ToDateTime(_DataRow["ActualStartDate"]);
            _Vendor_Project.Name = ConvertHelper.Tostring(_DataRow["Name"]);
            _Vendor_Project.ScheduledCost = ConvertHelper.Todouble(_DataRow["ScheduledCost"]);
            _Vendor_Project.ScheduledEndDate = ConvertHelper.ToDateTime(_DataRow["ScheduledEndDate"]);
            _Vendor_Project.ScheduledStartDate = ConvertHelper.ToDateTime(_DataRow["ScheduledStartDate"]);
            //_Vendor_Project.Status = new EProjectStatus(1);
            //_Vendor_Project.Type = new EProjectType(1);
            //_Vendor_Project.Vendor = LVendor.SelectData(ConvertHelper.ToInt32(_DataRow["VendorFK"]));
            _Vendor_Project.Vendor_ProjectId = ConvertHelper.ToInt32(_DataRow["Vendor_ProjectId"]);
            return _Vendor_Project;
        }
        public static List<EVendor_Project> SelectAll(string connectionKey)
        {
            DataTable _dt = DVendor_Project.SelectAll(connectionKey).Tables[0]; 
            List<EVendor_Project> _VendorProjectList = new List<EVendor_Project>();
            EVendor_Project _VendorProject;
            foreach (DataRow dr in _dt.Rows)
            {
                _VendorProject = SelectData(dr);
                _VendorProject.Vendor = new EVendor(ConvertHelper.ToInt32(dr["VendorFK"]), ConvertHelper.Tostring(dr["Vendor_Name"]),"1");
                _VendorProjectList.Add(_VendorProject);
            }
            return _VendorProjectList;
        }

        public static List<EVendor_Project> SelectAllIdName(DataTable dt)
        {
            List<EVendor_Project> _VendorProjectList = new List<EVendor_Project>();
            EVendor_Project _VendorProject;
            foreach (DataRow dr in dt.Rows)
            {
                _VendorProject = new EVendor_Project(ConvertHelper.Toint(dr["Vendor_ProjectID"]), dr["Name"].ToString());
                _VendorProject.Vendor = new EVendor(ConvertHelper.ToInt32(dr["VendorFK"]));
                _VendorProjectList.Add(_VendorProject);
            }
            return _VendorProjectList;
        }
        public static List<EVendor_Project> SelectAllIdName(string connectionKey)
        {
            DataTable dt = DVendor_Project.SelectAllIdName(connectionKey).Tables[0];
            return SelectAllIdName(dt);
        }
        public static List<EVendor_Project> SelectAllIdNameByVendor(int vendorID, string connectionKey)
        {
            DataTable dt = DVendor_Project.SelectAllIdNameByVendor(vendorID, connectionKey).Tables[0];
            return SelectAllIdName(dt);
        }

        public List<EMobileQuery> CreateInsertMobileQuery(string connectionKey)
        {
            List<EMobileQuery> listQueries = new List<EMobileQuery>();
            listQueries.Add(new EMobileQuery("Delete tb_Projects"));

            DataTable dt = DVendor_Project.SelectAllIdName(connectionKey).Tables[0];
            foreach (DataRow dr in dt.Rows)
            {
                int id = ConvertHelper.ToInt32(dr["Vendor_ProjectID"]);

                if (Vendor_ProjectIdsToFilter.Count > 0)
                {
                    if (Vendor_ProjectIdsToFilter.Find(x => x == id) > 0)
                        listQueries.Add(new EMobileQuery("insert into tb_Projects values (" + ConvertHelper.Toint(dr["Vendor_ProjectID"]) + "," + ConvertHelper.ToInt32(dr["VendorFK"]) + ",'" + dr["Name"].ToString() + "')"));
                }
                else
                {
                    listQueries.Add(new EMobileQuery("insert into tb_Projects values (" + ConvertHelper.Toint(dr["Vendor_ProjectID"]) + "," + ConvertHelper.ToInt32(dr["VendorFK"]) + ",'" + dr["Name"].ToString() + "')"));
                }
            }
            return listQueries;
        }
    }
}
